
import Base from "./base";
function ensureSlash() {
    if (window.location.hash) {
        return
    }
    window.location.hash = '/#'
}
function getHash() {
    return window.location.hash.slice(1) // #/about/a   截掉#

}
class HashHistory extends Base {

    constructor(router) {
        super(router)

        ensureSlash() // 初始化hash路由时   需要有个#
    }

    setupListener() {
        window.addEventListener('hashchange', () => {
            // console.log(getHash())
            this.transitionTo(getHash())
        })
        // window.addEventListener('popstate')
    }
    getCurrentLocation() {
        return window.location.pathname
    }
    push(location) {
        this.transitionTo(location, () => {
            window.location.hash = location
        })
    }


}

export default HashHistory